package cn.wildfire.chat.kit.user;

import android.os.RemoteException;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.pick.PickUserViewModel;
import cn.wildfire.chat.kit.group.BasePickGroupMemberActivity;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;
import cn.wildfirechat.remote.GeneralCallback;

public class BlackListActivity extends BasePickGroupMemberActivity {


    List<String> blackList = new ArrayList<>();
    private List<UIUserInfo> checkedReceiverMembers;
    // 显示总数据
    private List<UIUserInfo> checkList;
    private boolean onCreate;

    protected UserInfo selfUserInfo;


    @Override
    protected void onGroupMemberChecked(List<UIUserInfo> checkedUserInfos) {
        this.checkedReceiverMembers = checkedUserInfos;
    }

    @Override
    protected int menu() {
        return 0;
    }

    public void getBlackListUsers() {
        try {
            blackList = ChatManager.getRemoteClient().getBlackList(true);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void beforeViews() {
        super.beforeViews();
        getBlackListUsers();

        onCreate = true;
        pickUserViewModel = ViewModelProviders.of(this).get(PickUserViewModel.class);
        selfUserInfo = ChatManager.Instance().getUserInfo(ChatManager.Instance().getUserId(), false);
    }

    @Override
    protected void afterViews() {
        isNotGroupChoose = true;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.text_blacklist);
        setSearchLayoutVisibility(false);
        getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
        getRightTxtView().setTextColor(getResources().getColor(R.color.white));



        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.text_edit), new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (getRightTxtView().getText().toString().equals(getString(R.string.text_edit))){
                    getRightTxtView().setText(getString(R.string.move_out_from_blacklist));
                    forceShowSelectorWithNotGroupSet = true;
                    isNotGroupChoose = false;
                    for (UIUserInfo uiUserInfo : checkList) {
                        uiUserInfo.setChecked(false);
                    }
                    pickUserViewModel.setUsers(checkList);
                    initView();
                }else if (getRightTxtView().getText().toString().equals(getString(R.string.move_out_from_blacklist))){
                    /**
                     * 对比查询需要移出黑名单列表
                     * */
                    if (checkedReceiverMembers != null && checkedReceiverMembers.size() > 0) {
                        List<UIUserInfo> checkedList = new ArrayList<>();
                        for (UIUserInfo uiUserInfo : checkList) {
                            if (uiUserInfo.isChecked()){
                                checkedList.add(uiUserInfo);
                            }
                        }

                        for (int i = 1; i <= checkedList.size(); i++) {
                            UIUserInfo uiUserInfo = checkedList.get(i - 1);
                            int finalI = i;
                            ChatManager.Instance().setBlackList(uiUserInfo.getUserInfo().uid, false, new GeneralCallback() {
                                @Override
                                public void onSuccess() {
                                    if (finalI == checkedList.size()){
                                        ToastTip.tip(getString(R.string.move_out_from_blacklist_success));
                                        finish();
                                    }
                                }

                                @Override
                                public void onFail(int errorCode) {
                                    ToastTip.tip(getString(R.string.move_out_from_blacklist_failure));
                                    finish();
                                }
                            });
                        }

                    }
                }

            }
        });

        if (blackList== null || blackList.size() == 0){
            getRightTxtView().setVisibility(View.GONE);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        try {
            /**
             * 发送名片只能给一人
             * */
            if (hasFocus && onCreate) {
                onCreate = false;
                pickUserViewModel.setInitialCheckedIds(new ArrayList<>());
                bindListener();
                List<UserInfo> userInfoList = ChatManager.Instance().getUserInfos(blackList, null);
                List<UserInfo> tempCheckList = new ArrayList<>();
                for (int i = 0; i < userInfoList.size(); i++) {
                    UserInfo userInfo = userInfoList.get(i);
                    if (!(userInfo.uid.equals(selfUserInfo.uid))) {
                        tempCheckList.add(userInfo);
                    }
                }
                checkList = UIUserInfo.userInfoToUIUserInfo(tempCheckList);
                pickUserViewModel.setUsers(checkList);
                forceShowSelectorWithNotGroupSet = false;
                isNotGroupChoose = false;
                initView();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (pickGroupMemberFragment != null) {
                            pickGroupMemberFragment.showQuickIndexBar(true);
                        }
                    }
                }, 500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

